﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string maintain_Add(maintain model ,int ACID);


[OperationContract]
string maintain_Update(maintain model ,int ACID);


[OperationContract]
string maintainBypid_Delete(string pid,int ACID);


[OperationContract]
maintain maintainBypid_Get(string pid,int ACID );


[OperationContract]
List<maintain> maintain_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加maintain表数据
/// <summary>
/// 添加maintain
/// </summary>
/// <param name="model">maintain实体</param>
/// <returns>空或错误</returns>
public string maintain_Add(maintain model,int ACID)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『数据维护单据号』不能为空！";
}
if(model.F_isfinished==null )
{
return "『付款完成标志』不能为空！";
}
if(model.F_balance==null )
{
return "『应付余额』不能为空！";
}
if(model.F_discount==null )
{
return "『应付折让金额』不能为空！";
}
if(model.F_total==null )
{
return "『应付总金额』不能为空！";
}
if(model.S_isfinished==null )
{
return "『收款完成标志』不能为空！";
}
if(model.S_balance==null )
{
return "『应收余额』不能为空！";
}
if(model.S_discount==null )
{
return "『应收折让金额』不能为空！";
}
if(model.S_total==null )
{
return "『应收总金额』不能为空！";
}
if(model.ischeck==null )
{
return "『复核标志』不能为空！";
}
if(model.Pdate ==null )
{
return "『开单日期』不能为空！";
}
if(string.IsNullOrEmpty(model.phone))
{
return "『号码』不能为空！";
}
if(model.gysID==null || model.gysID.Equals(Guid.Empty))
{
return "『供应商ID』不能为空！";
}
if(model.clientID==null || model.clientID.Equals(Guid.Empty))
{
return "『客户编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.maintain.Count(p=>p.pid==model.pid)>0)
{
return "主键已存在！";
}
ctx.maintain.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加maintain表数据事物用重构
/// <summary>
/// 添加maintain事物用重构
/// </summary>
/// <param name="model">maintain实体</param>
/// <returns>空或错误</returns>
private string maintain_Add(maintain model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『数据维护单据号』不能为空！";
}
if(model.F_isfinished==null )
{
return "『付款完成标志』不能为空！";
}
if(model.F_balance==null )
{
return "『应付余额』不能为空！";
}
if(model.F_discount==null )
{
return "『应付折让金额』不能为空！";
}
if(model.F_total==null )
{
return "『应付总金额』不能为空！";
}
if(model.S_isfinished==null )
{
return "『收款完成标志』不能为空！";
}
if(model.S_balance==null )
{
return "『应收余额』不能为空！";
}
if(model.S_discount==null )
{
return "『应收折让金额』不能为空！";
}
if(model.S_total==null )
{
return "『应收总金额』不能为空！";
}
if(model.ischeck==null )
{
return "『复核标志』不能为空！";
}
if(model.Pdate ==null )
{
return "『开单日期』不能为空！";
}
if(string.IsNullOrEmpty(model.phone))
{
return "『号码』不能为空！";
}
if(model.gysID==null || model.gysID.Equals(Guid.Empty))
{
return "『供应商ID』不能为空！";
}
if(model.clientID==null || model.clientID.Equals(Guid.Empty))
{
return "『客户编号』不能为空！";
}
if(ctx.maintain.Count(p=>p.pid==model.pid)>0)
{
return "主键已存在！";
}
ctx.maintain.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改maintain表数据
/// <summary>
/// 修改maintain
/// </summary>
/// <param name="model">maintain实体</param>
/// <returns>空或错误</returns>
public string maintain_Update(maintain model,int ACID)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『数据维护单据号』不能为空！";
}
if(model.F_isfinished==null )
{
return "『付款完成标志』不能为空！";
}
if(model.F_balance==null )
{
return "『应付余额』不能为空！";
}
if(model.F_discount==null )
{
return "『应付折让金额』不能为空！";
}
if(model.F_total==null )
{
return "『应付总金额』不能为空！";
}
if(model.S_isfinished==null )
{
return "『收款完成标志』不能为空！";
}
if(model.S_balance==null )
{
return "『应收余额』不能为空！";
}
if(model.S_discount==null )
{
return "『应收折让金额』不能为空！";
}
if(model.S_total==null )
{
return "『应收总金额』不能为空！";
}
if(model.ischeck==null )
{
return "『复核标志』不能为空！";
}
if(model.Pdate ==null )
{
return "『开单日期』不能为空！";
}
if(string.IsNullOrEmpty(model.phone))
{
return "『号码』不能为空！";
}
if(model.gysID==null || model.gysID.Equals(Guid.Empty))
{
return "『供应商ID』不能为空！";
}
if(model.clientID==null || model.clientID.Equals(Guid.Empty))
{
return "『客户编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.maintain.Count(p=>p.pid==model.pid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
maintain maintainmodel=ctx.maintain.Single(p => p.pid==model.pid);
if(!string.IsNullOrEmpty(model.pid))
{
maintainmodel.pid=model.pid;
}
if(model.clientID!=null || model.clientID.Equals(Guid.Empty))
{
maintainmodel.clientID=model.clientID;
}
if(model.gysID!=null || model.gysID.Equals(Guid.Empty))
{
maintainmodel.gysID=model.gysID;
}
if(!string.IsNullOrEmpty(model.phone))
{
maintainmodel.phone=model.phone;
}
if(model.Pdate!=null )
{
maintainmodel.Pdate=model.Pdate;
}
if(model.ischeck!=null )
{
maintainmodel.ischeck=model.ischeck;
}
if(model.S_total!=null )
{
maintainmodel.S_total=model.S_total;
}
if(model.S_discount!=null )
{
maintainmodel.S_discount=model.S_discount;
}
if(model.S_balance!=null )
{
maintainmodel.S_balance=model.S_balance;
}
if(model.S_isfinished!=null )
{
maintainmodel.S_isfinished=model.S_isfinished;
}
if(model.F_total!=null )
{
maintainmodel.F_total=model.F_total;
}
if(model.F_discount!=null )
{
maintainmodel.F_discount=model.F_discount;
}
if(model.F_balance!=null )
{
maintainmodel.F_balance=model.F_balance;
}
if(model.F_isfinished!=null )
{
maintainmodel.F_isfinished=model.F_isfinished;
}
if(!string.IsNullOrEmpty(model.custom2))
{
maintainmodel.custom2=model.custom2;
}
if(!string.IsNullOrEmpty(model.custom1))
{
maintainmodel.custom1=model.custom1;
}
if(!string.IsNullOrEmpty(model.skd))
{
maintainmodel.skd=model.skd;
}
if(!string.IsNullOrEmpty(model.fkd))
{
maintainmodel.fkd=model.fkd;
}
if(!string.IsNullOrEmpty(model.opencardpid))
{
maintainmodel.opencardpid=model.opencardpid;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除maintain表Bypid数据
/// <summary>
/// 删除maintainBypid
/// </summary>
/// <param name="pid">数据维护单据号</param>
/// <returns>空或错误</returns>
public string maintainBypid_Delete(string pid,int ACID)
{
if(string.IsNullOrEmpty(pid))
{
return "『数据维护单据号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.maintain.Count(p=>p.pid==pid)!=1)
{
return null;
}
maintain model=ctx.maintain.Single(p => p.pid== pid);
ctx.maintain.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除maintain表Bypid数据[事物用重构]
/// <summary>
/// 删除maintainBypid[事物用重构]
/// </summary>
/// <param name="pid">数据维护单据号</param>
/// <returns>空或错误</returns>
private string maintainBypid_Delete(string pid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(pid))
{
return "『数据维护单据号』不能为空！";
}
if(ctx.maintain.Count(p=>p.pid==pid)!=1)
{
return null;
}
maintain model=ctx.maintain.Single(p => p.pid== pid);
ctx.maintain.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取maintain表Bypid数据
/// <summary>
/// 获取maintainBypid
/// </summary>
/// <param name="pid">数据维护单据号</param>
/// <returns>当前表</returns>
public maintain maintainBypid_Get(string pid,int ACID)
{
if(string.IsNullOrEmpty(pid))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.maintain.Count(p=>p.pid==pid)!=1)
{
return null;
}
return ctx.maintain.SingleOrDefault(p=>p.pid==pid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取maintain表数据
/// <summary>
/// 获取maintain
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<maintain></returns>
public List<maintain> maintain_Get(Expression<Func<maintain, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.maintain.ToList();
}
 else 
{
return ctx.maintain.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.maintain.Where(predicate).ToList();
}
 else 
{
return ctx.maintain.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取maintain表数据
/// <summary>
/// 获取maintain
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<maintain></returns>
public List<maintain> maintain_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return maintain_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取maintain表数据计数
/// <summary>
/// 获取maintain表数据计数
/// </summary>
/// <returns>List<maintain></returns>
public int maintain_GetCount(Expression<Func<maintain, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.maintain.Count();

}
 else
{
return ctx.maintain.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取maintain分页表数据
/// <summary>
/// 获取maintain分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<maintain></returns>
public List<maintain> maintain_Getpage(int pageIndex, int pageSize,Expression<Func<maintain, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.maintain.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.maintain.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.maintain.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.maintain.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改maintain表数据
/// <summary>
/// 批量修改maintain
/// </summary>
/// <param name="predicate">Expression<Func<maintain, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<maintain,maintain>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int maintain_batchUpdate(Expression<Func<maintain, bool>> predicate,Expression<Func<maintain, maintain>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.maintain.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改maintain表数据事物用重构
/// <summary>
/// 批量修改maintain事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<maintain, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<maintain,maintain>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int maintain_batchUpdate(Expression<Func<maintain, bool>> predicate,Expression<Func<maintain, maintain>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.maintain.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除maintain表数据
/// <summary>
/// 批量删除maintain
/// </summary>
/// <param name="predicate">Expression<Func<maintain, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int maintain_batchDel(Expression<Func<maintain, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.maintain.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除maintain表数据事物用重构
/// <summary>
/// 批量删除maintain事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<maintain, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int maintain_batchDel(Expression<Func<maintain, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.maintain.Delete(predicate);
return ok;
}
#endregion


}
}

